package com.example.springboot3demo.controller;

import com.example.springboot3demo.test.BaseResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Slf4j
@Tag(name = "工具类请求接口",description = "工具类请求接口")
@RequestMapping("/tools")
public class ToolsController {

    @PostMapping("/upload")
    @Operation(summary = "文件上传", description = "上传一个文件并打印其字节内容")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "-1", description = "非200失败")
    })
    public BaseResponse<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            String content = new String(file.getBytes(), java.nio.charset.StandardCharsets.UTF_8);
            log.info("文件内容: {}", content);
            return new BaseResponse<String>(content);
        } catch (Exception e) {
            log.error("文件上传失败", e);
            return new BaseResponse<String>(null);
        }
    }


}
